package top.lywivan.bio;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @desc
 * @author:ivan.liu
 */
public class BioServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8887);
        while (true){
            Socket socket = serverSocket.accept();
            new Thread(new SingleHandler(socket)).start();
        }
    }

    static class SingleHandler implements Runnable{

        private Socket socket;

        //装饰器模式
        public SingleHandler(Socket socket){
            this.socket=socket;
        }

        @Override
        public void run() {
            try {
                InputStream inputStream = socket.getInputStream();// 读数据
                OutputStream outputStream = socket.getOutputStream();//向客户端发送数据
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));
                //读数据
                while (true) {
                    String line = reader.readLine();
                    System.out.println(line);
                    // 做业务操作 。。。。。。。。。。。

                    writer.write("hello client ,iam bioserver \n");
                    writer.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
